package org.ccu.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.ccu.common.ConnectionManager;

public class UserDAO {
	private static Connection conn = ConnectionManager.getConnection();
	private static Statement st = null;
	private static ResultSet rs = null;

	public static boolean registUser(String username, String password) {

		try {
			st = conn.createStatement();
			String sql = "insert into myu(user_id,user_name,user_password) values((select max(user_id)+1 from myu),'"+username+"','"+password+"')";
			System.out.println(sql);
			st.execute(sql);
			
		
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		} finally {
			try {
				st.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return true;

	}

	public static boolean vertifyUser(String username, String password) {

		String temp = null;

		try {
			st = conn.createStatement();
			String sql = "select user_password from myu where user_name='"
					+ username + "'";
			System.out.println(sql);
			rs = st.executeQuery(sql);

			while (rs.next()) {

				temp = rs.getString(1);

			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			Boolean b=ConnectionManager.close(st, rs);
		}
		if (temp != null && temp.equals(password)) {
			return true;
		}

		return false;

	}
}
